// pages/apply/factoryinfo/index.js
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		factory_id:'',
		factory:'',
		isZan:false,
		id:'',
		baseUrl:app.globalData.baseUrl
	},
	call() {
		wx.showModal({
			title: '联系平台',
			content: app.globalData.concat,
			success(res) {
				if (res.confirm) {
					wx.makePhoneCall({
						phoneNumber: app.globalData.concat //仅为示例，并非真实的电话号码
					})
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},
	onSubmit() {
		let that = this;
		wx.showLoading({
			title:'提交中',
			mask: true
		})
		wx.request({
			url: app.globalData.baseUrl + '/api/apply/appointRepairFactory',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				order_id: that.data.id,
				repair_factory_id: that.data.factory_id
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					wx.showModal({
						title: '提示',
						content: '提交成功',
						showCancel: false,
						confirmColor:'#DC5640',
						confirmText:'确定',
						success(res) {
							wx.switchTab({
								url:'/pages/index/index'
							})
						}
					})
				}else {
					wx.showModal({
						title: '提示',
						content: '提交失败',
						showCancel: false,
						confirmColor:'#DC5640',
						confirmText:'确定',
						success(res) {
							
						}
					})
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	onGetCocact(e) {
		let phone = e.currentTarget.dataset.phone
		console.log(phone)
		wx.makePhoneCall({
			phoneNumber: phone //仅为示例，并非真实的电话号码
		})
	},
	onZan(e){
		var that = this;
		let id = e.currentTarget.dataset.id;

		wx.request({
			url: app.globalData.baseUrl + '/api/repair/zan',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				id:id
			},
			success: function(res) {
				if (res.data.status) {
					that.setData({
						isZan:true
					})
				}else {
					
				}
			},
			fail: function() {
				console.log("请求失败")
			}
		})
	},
	onGetFactory(id){
		var that = this;
		wx.showLoading({
			title:'加载中',
			mask: true
		})
		console.log(id)
		wx.request({
			url: app.globalData.baseUrl + '/api/repair/detail',
			method: 'GET',
			header: {
				'content-type': 'application/x-www-form-urlencoded',
				'token': app.globalData.token,
			},
			data: {
				id:id
			},
			success: function(res) {
				wx.hideLoading()
				if (res.data.status) {
					that.setData({
						factory:res.data.data.records
					})
				}else {
					
				}
			},
			fail: function() {
				wx.hideLoading()
				console.log("请求失败")
			}
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		
		this.onGetFactory(options.factory_id);
		this.setData({
			id:options.id,
			factory_id:options.factory_id
		})
	},
	onNaigator(){
		let plugin = requirePlugin('routePlan');
		let key = 'A6YBZ-H7J6I-V6DGF-5D7A5-IVEOS-DXB63'; //使用在腾讯位置服务申请的key
		let referer = 'baojiayoupei'; //调用插件的app的名称
		console.log( this.data.factory)
		let endPoint = JSON.stringify({ //终点
		 'name':  this.data.factory.name,
		 'latitude': this.data.factory.lat,
		 'longitude': this.data.factory.lng
		});
		wx.navigateTo({
		 url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
		});
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
